/*
 * 朗日信息技术有限公司版权所有 2001-2005}
 * 本源代码是朗日信息技术有限公司的财产,
 * 受《著作权法》、《中华人民共和国软件保护条例》及《国际版权公约》保护, 
 * 任何企业或者个人未经朗日信息技术有限公司的明确书面授权许可,
 * 不得复制、销售本源代码给非本企业人员,不得对本源代码及其拷贝进行修改, 
 * 不得在任何公共媒体上散播本源代码，不得把本源代码编译成目标软件,
 * 不得复制本源代码全部或部分到其他非本企业的软件产品中。
 * 
 * 
 * Copyright (c) 2001-2005 by Luminary Technology LTD. China. 
 * All rights reserved.
 * 
 * 创建日期 2005-11-17
 * @author Smart
 */
package com.smartdio.company;

import java.util.MissingResourceException;
import java.util.ResourceBundle;

import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.CoolBar;
import org.eclipse.swt.widgets.CoolItem;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Item;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Link;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.swt.widgets.Widget;

/**
 * 提供简化的国际化支持。 控件上的文字只要是"%"开头的即可以自动翻译为国际化字符
 * 
 * @author Smart
 * 
 */
public class WidgetMessagesUtil {

	public static String getString(String key, ResourceBundle bundle) {
		// TODO Auto-generated method stub
		try {
			return bundle.getString(key);
		} catch (MissingResourceException e) {
			return '!' + key + '!';
		}
	}

	/**
	 * 国际化某个控件
	 * 
	 * @param control
	 */
	public static void i18nWidget(Widget control, ResourceBundle bundle) {
		if (control instanceof Control) {

			if (control instanceof Button) {
				Button btn = (Button) control;
				if("".equals(btn.getText())){
					
				}else{
				Image image=btn.getImage();
				btn.setText(converString(btn.getText(), bundle));
				btn.setImage(image);
				}
				btn.setToolTipText(converString(btn.getToolTipText(), bundle));
			} else if (control instanceof Label) {
				Label lbl = (Label) control;
				lbl.setText(converString(lbl.getText(), bundle));
				lbl.setToolTipText(converString(lbl.getToolTipText(), bundle));

			} else if (control instanceof Text) {
				Text txt = (Text) control;
				txt.setText(converString(txt.getText(), bundle));
				txt.setToolTipText(converString(txt.getToolTipText(), bundle));
			} else if (control instanceof Group) {
				Group grp = (Group) control;
				grp.setText(converString(grp.getText(), bundle));
				grp.setToolTipText(converString(grp.getToolTipText(), bundle,grp.getText()));
			} else if (control instanceof Link) {
				Link lnk = (Link) control;
				lnk.setText(converString(lnk.getText(), bundle));
				lnk.setToolTipText(converString(lnk.getToolTipText(), bundle,lnk.getText()));
			}

		} else if (control instanceof Item) {
			Item item = (Item) control;
			if ("".equals(item.getText())) {

			} else {
				Image image = item.getImage();
				item.setText(converString(item.getText(), bundle));
			
				if (image != null) {
					item.setImage(image);
				}
			}

			if (control instanceof ToolItem) {
				ToolItem tItem = (ToolItem) control;
				tItem.setToolTipText(converString(tItem.getToolTipText(),
						bundle,tItem.getText()));
			} else if (control instanceof TabItem) {
				TabItem tItem = (TabItem) control;
				tItem.setToolTipText(converString(tItem.getToolTipText(),
						bundle,tItem.getText()));
			}
		}
	}

	/**
	 * 国际化指定的控件，以及该控件的子控件
	 * 
	 * @param control
	 */
	public static void i18nWidgetWithChilds(Widget control,
			ResourceBundle bundle) {
		i18nWidget(control, bundle);
		if (control instanceof Composite) {
			Control[] childs = ((Composite) control).getChildren();
			for (int i = 0; i < childs.length; i++) {
				i18nWidgetWithChilds(childs[i], bundle);
			}
		}
		if (control instanceof ToolBar) {
			ToolItem[] childs = ((ToolBar) control).getItems();
			for (int i = 0; i < childs.length; i++) {
				i18nWidgetWithChilds(childs[i], bundle);
			}
		}
		if (control instanceof CoolBar) {
			CoolItem[] childs = ((CoolBar) control).getItems();
			for (int i = 0; i < childs.length; i++) {
				i18nWidgetWithChilds(childs[i], bundle);
			}
		}
		if (control instanceof TabFolder) {
			TabItem[] childs = ((TabFolder) control).getItems();
			for (int i = 0; i < childs.length; i++) {
				i18nWidgetWithChilds(childs[i], bundle);
			}
		}
	}

	public static String converString(String string, ResourceBundle bundle) {
		if (string == null)
			return "";
		if (string.length() > 2) {
			char c = string.charAt(0);
			if (c == '%') {
				return getString(string.substring(1, string.length()), bundle);
			} else {
				return string;
			}
		} else {
			return string;
		}
	}
	
	public static String converString(String string, ResourceBundle bundle,String defaultMessage) {
		if (string == null)
			return defaultMessage;
		if("".equals(string)){
			return defaultMessage;
		}
		if (string.length() > 2) {
			char c = string.charAt(0);
			if (c == '%') {
				return getString(string.substring(1, string.length()), bundle);
			} else {
				return string;
			}
		} else {
			return string;
		}
	}

}
